/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Nobita Chau
 */
public class DBConnect {
//    public static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//    public static String url = "jdbc:sqlserver://NOBITACHAU:1433;databaseName=QUANLYGATAU";
//    public static String user = "sa";
//    public static String pass = "1";
//    public static Connection cnn;
//
//    public static boolean open() {
//        try {
//            if (cnn == null || cnn.isClosed()) {
//                Class.forName(driver);
//                cnn = DriverManager.getConnection(url, user, pass);
//            }
//            return true;
//        } catch (SQLException ex) {
//            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (ClassNotFoundException ex) {
//            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        return false;
//    }
//
//    public static void close() {
//        try {
//            if (cnn != null) {
//                cnn.close();
//            }
//        } catch (SQLException ex) {
//            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        
//    }
//    
//    public static void close(PreparedStatement ps){
//        try {
//            if (ps !=null) {
//            ps.close();
//            }
//        } catch (SQLException ex) {
//            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        close();
//    }
//    public static void close(PreparedStatement ps, ResultSet rs){
//        try {
//            if (rs !=null) {
//            rs.close();
//            }
//        } catch (SQLException ex) {
//            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        close(ps);
//    }
      public  Connection connection;

   public static Connection Connect() {
        try {
            String driver = "com.mysql.jdbc.Driver";
            String url = "jdbc:mysql://localhost:3306/";
            String tv ="?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true";
            String username = "root";
            String password = "";
            String dbName = "quanlyduongsat";
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url + dbName+tv, username, password);
            return conn;
        } catch (Exception ex) {

            return null;
        }
    }

    public void close() {
        try {
            this.connection.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    public ResultSet executeQuery(String sql) {
        ResultSet rs = null;
        try {
            Statement sm = this.connection.createStatement();
            rs = sm.executeQuery(sql);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return rs;
    }

    public int executeUpdate(String sql) {
        int n = -1;
        try {
            Statement sm = this.connection.createStatement();
            n = sm.executeUpdate(sql);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return n;
    }
    
    
}
